{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Building an image recognition neural network"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from tensorflow import keras\n",
    "\n",
    "# Setting random seeds to get reproducible results\n",
    "np.random.seed(0)\n",
    "import tensorflow as tf\n",
    "tf.random.set_seed(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Importing and reading the dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Size of the training set 60000\n",
      "Size of the testing set 10000\n"
     ]
    }
   ],
   "source": [
    "(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()\n",
    "print(\"Size of the training set\", len(x_train))\n",
    "print(\"Size of the testing set\", len(x_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The label is 2\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAOgklEQVR4nO3dfayU5ZnH8d8FtBp5iShHciJkDxKNSxaX1gmucVNZiRXUBBtTLcbKGiKNb2mTJmq6CfUPTci6FElcUFgRtnQhxNaIL9mtgUYCUeJgWDwu8WUNlAPIOWgEiUA5cO0f56F7imfuOczzzItc308ymZnnmvs8F8P5nWdm7pm5zd0F4Nw3pNkNAGgMwg4EQdiBIAg7EARhB4IY1sidjRkzxjs6Ohq5SyCUX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yOdscx4y9x3tSx/nlczwj7QUlKtNP93nbt/V9JMSQ9mD1cxOINaxrtRBlhmvCXUuvx5Xs0Ie5ek8f2uj5O0rwl9DMjd92Xn3ZJeUustRX3g9Aq62Xl3k/v5s1ZaxnugZcbVAvddM5c/b0bY35F0uZlNMLNvS/qRpPVN6ONrzGx49sKJzGy4pO+r9ZaiXi9pTnZ5jqSXm9jLX2iVZbwrLTOuJt93TV/+3N0bfpJ0s/pekf9fSf/UjB4q9HWZpP/OTu83uzdJa9T3sO6E+h4RzZV0saQNkj7Kzi9qod5+Lek9STvUF6z2JvX29+p7arhD0vbsdHOz77tEXw2533i7LBAE76ADgiDsQBCEHQiCsANBEHYgCMIOBEHYgSD+D4wEX235CE6/AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(x_train[5], cmap='Greys')\n",
    "print(\"The label is\", y_train[5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAABG0AAADiCAYAAAD0xzrZAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAaJUlEQVR4nO3deZBdVb0v8LUSyMxgIATwBhuBAIq8gGFQw3sRkLFUBhlUEARR4sAgQxSKQQICWjwGgyIRiIKlaES8eHmCIjIoKlLIwzAqJISkAsmD4E0kCMl+f3RzL8bz26ZPn+6zOv35VHVVsr699/7tJCt98uud88tVVSUAAAAAyjKo3QUAAAAA8M80bQAAAAAKpGkDAAAAUCBNGwAAAIACadoAAAAAFEjTBgAAAKBAmjYFyTn/Kuf8ib4+Fqhnb0KZ7E0ok70JZ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Onhlmz/7aNnHTSSS09X0op3XjjjWE2YsSIll+vdJ60AQAAACiQpg0AAABAgTRtAAAAAAqkaQMAAABQIE0bAAAAgAJp2gAAAAAUyMjvXlQ3/uz3v/99mP34xz9uuD579uzwmO222271C4PCvPbaa2F26qmnhtkNN9wQZuutt16Y3XbbbWG25ZZbhtmrr74aZmuyp59+ut0l0OVPf/pTt48pabT8muzMM88MswULFoTZ9ttvH2ZDhgzpUU3QTkuWLGm4/sEPfrDl1zr33HPDbJtttmn59aCRq666KszqxnrXjeA+/PDDw+yLX/ximK299tphFql7Pf7QQw+F2ZNPPhlmVVWFWd0Y9IkTJ4bZQORJGwAAAIACadoAAAAAFEjTBgAAAKBAmjYAAAAABdK0AQAAACiQpg0AAABAgYz87kV1ozqvvvrqMLvjjjsarteNSDzggAPC7D3veU+YHXjggWGWcw4zaKVnnnkmzOrGetf57W9/G2bjx49v6pzDhw9v6jhop1122aXdJRTnlVdeCbMHHnggzOq+dt94441N1VI38nTYsGFNnRNKcM899zRc/81vftPU+Q455JAwO/roo5s6J3TX8uXLw2zatGlhVvfvqrqx3tdee+3qFdYNL7zwQsP1ww47LDzmzjvvbOpan/rUp8LsuOOOa+qcA5EnbQAAAAAKpGkDAAAAUCBNGwAAAIACadoAAAAAFEjTBgAAAKBApke1yejRo8Pstttua7i+zz77hMdcdtllTWV170h+8MEHh9moUaPCDLrrM5/5TJhVVRVmddPPmp0QtSZbuXJlmA0aFPfw634PKN+SJUv69HoLFiwIs7o/g3fddVeYPf3002H297//veH61772tfCYFStWhNnIkSPDbK+99gqzuklPr776aphtu+22YQalu//++8PsqKOO6vb53v/+94fZjBkzwsykNfpK3deP5557rqlzXnrppWG2bNmyMJs1a1aY1U00vO+++xqu//Wvfw2PqZt+VZd94hOfCLO6Scv8I0/aAAAAABRI0wYAAACgQJo2AAAAAAXStAEAAAAokKYNAAAAQIE0bQAAAAAKZOR3gXbeeeeG67Nnzw6POfnkk8Pshz/8YZgdc8wxYfaXv/wlzE477bQw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AAABRI0wYAAACgQJo2AAAAAAXStAEAAAAokKYNAAAAQIE0bQAAAAAKZOR3myxatCjMrrzyyobr1113XXjMs88+2+OaVjV48OAw6+joCLO6kXAMXFVVNZXNnDkzzM4666yelNQnvve974XZ5z73uTB78cUXw+yEE04Is0svvXT1CgOALs8//3yY1Y0BrjN16tSG60OGDGnqfEBrvOMd72h3CXSTJ20AAAAACqRpAwAAAFAgTRsAAACAAmnaAAAAABRI0wYAAACgQJo2AAAAAAUy8ruHli5dGma33HJLmJ133nlh9sQTT/Sopu7Yfffdw+yiiy4Ks3e+8529UQ5rsLpR8HVZ3Tj7un107LHHhtk666wTZrNnzw6zb37zmw3X77nnnvCYOXPmhNkWW2wRZocffniY1Y38Btqjqqowmzt3bpi99a1v7Y1y4J+ceuqpYbZy5cqWX2/77bdv+TmBnnv44YfbXQLd5EkbAAAAgAJp2gAAAAAUSNMGAAAAoECaNgAAAAAF0rQBAAAAKJCmDQAAAECBjPzusmzZsjCbN29emB1xxBFh9uCDD/aopu7Ya6+9wuxLX/pSmO20005hVjeGGfrKihUrwqxu5Pc111wTZqNHjw6zVo9B3HfffcNsn332CbPPfvazLa0D6F11XzN7Y5wyNDJ//vwwmzVrVpgNGhR/H3fo0KFhds4554TZyJEjwwxon6eeeqrdJdBNnrQBAAAAKJCmDQAAAECBNG0AAAAACqRpAwAAAFAgTRsAAACAAmnaAAAAABRojRv5/fLLL4fZSSedFGb33ntvmD322GM9qqm79ttvv4brZ599dnjMhAkTwmzttdfucU3QU29/+9vDbM899wyzX/ziF01d79lnnw2zupGodTbaaKOG61OmTAmPOeuss5q6FrDm+OUvfxlme+yxRx9Wwppu6dKlYdbs176Ojo4wmzp1alPnBNpn5513bri+cuXK8JhBgzzr0U5+9QEAAAAKpGkDAAAAUCBNGwAAAIACadoAAAAAFEjTBgAAAKBAmjYAAAAABSp65PecOXMarn/5y18Oj6kbDzx37tyeltQtI0aMCLNp06aF2ac//emG60OGDOlxTdAu6667bpjNmjUrzL7zne+E2QknnNCjmho5//zzw+y4445ruL7BBhu0vA6gf6mqqt0lAMC/tMkmmzRc32677cJjHn300TB77rnnwmzzzTdf/cIIedIGAAAAoECaNgAAAAAF0rQBAAAAKJCmDQAAAECBNG0AAAAAClT09Kgf/ehHDdevueaall9rxx13DLMPf/jDYbbWWvEv4Sc/+ckwGzZs2OoVBgPAqFGjwiyapvavMoBWO/jgg8Psqquu6sNKoLE3v/nNYbb//vuH2S233NIb5QD9yGWXXRZme++9d5idfvrpYTZ9+vQwGzt27OoVhidtAAAAAEqkaQMAAABQIE0bAAAAgAJp2gAAAAAUSNMGAAAAoECaNgAAAAAFKnrk9ymnnNKtdQCA3rLHHnuE2cqVK/uwEmhs1KhRYXbzzTf3YSVAfzNp0qQwO/TQQ8PsBz/4QZhtuOGGYXb55ZeH2ZAhQ8JsIPKkDQAAAECBNG0AAAAACqRpAwAAAFAgTRsAAACAAmnaAAAAABRI0wYAAACgQEWP/AYAAAB619ChQ8PsuuuuC7Ott946zKZNmxZm5557bpiNHTs2zAYiT9oAAAAAFEjTBgAAAKBAmjYAAAAABdK0AQAAACiQpg0AAABAgTRtAAAAAApk5DcAAADQUN048HPOOaepjNXnSRsAAACAAmnaAAAAABRI0wYAAACgQJo2AAAAAAXStAEAAAAokKYNAAAAQIFyVVWr/8k5L0opze29cqBob6mqaky7i2jE3mSAK3Jv2pdgb0Kh7E0oU8O92a2mDQAAAAB9w3+PAgAAACiQpg0AAABAgTRtAAAAAAqkaQMAAABQIE0bAAAAgAJp2gAAAAAUSNMGAAAAoECaNgAAAAAF0rQBAAAAKND/B/F2XvggbTP7AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 1440x1440 with 5 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig = plt.figure(figsize=(20,20))\n",
    "for i in range(5):\n",
    "    ax = fig.add_subplot(1, 5, i+1, xticks=[], yticks=[])\n",
    "    ax.imshow(x_train[i], cmap='Greys')\n",
    "    ax.set_title('Label:' + str(y_train[i]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Pre-processing the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Reshaping the features.\n",
    "# In the reshape function we use the -1 as a placeholder for the size of the dataset.\n",
    "\n",
    "x_train_reshaped = x_train.reshape(-1, 28*28)\n",
    "x_test_reshaped = x_test.reshape(-1, 28*28)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorflow.keras.utils import to_categorical\n",
    "y_train_cat = to_categorical(y_train, 10)\n",
    "y_test_cat = to_categorical(y_test, 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Building and training the neural network"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential_3\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "dense_9 (Dense)              (None, 128)               100480    \n",
      "_________________________________________________________________\n",
      "dropout_6 (Dropout)          (None, 128)               0         \n",
      "_________________________________________________________________\n",
      "dense_10 (Dense)             (None, 64)                8256      \n",
      "_________________________________________________________________\n",
      "dropout_7 (Dropout)          (None, 64)                0         \n",
      "_________________________________________________________________\n",
      "dense_11 (Dense)             (None, 10)                650       \n",
      "=================================================================\n",
      "Total params: 109,386\n",
      "Trainable params: 109,386\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "# Imports\n",
    "#import numpy as np\n",
    "from tensorflow.keras.models import Sequential\n",
    "from tensorflow.keras.layers import Dense, Dropout\n",
    "#from tensorflow.keras.layers import Dense, Dropout, Activation\n",
    "#from tensorflow.keras.optimizers import SGD\n",
    "\n",
    "# Building the model\n",
    "model = Sequential()\n",
    "model.add(Dense(128, activation='relu', input_shape=(28*28,)))\n",
    "model.add(Dropout(.2))\n",
    "model.add(Dense(64, activation='relu'))\n",
    "model.add(Dropout(.2))\n",
    "model.add(Dense(10, activation='softmax'))\n",
    "\n",
    "# Compiling the model\n",
    "model.compile(loss = 'categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "6000/6000 [==============================] - 12s 2ms/step - loss: 2.0735 - accuracy: 0.7010 1s - loss: 2.2\n",
      "Epoch 2/10\n",
      "6000/6000 [==============================] - 12s 2ms/step - loss: 0.5679 - accuracy: 0.8475\n",
      "Epoch 3/10\n",
      "6000/6000 [==============================] - 10s 2ms/step - loss: 0.4456 - accuracy: 0.8844\n",
      "Epoch 4/10\n",
      "6000/6000 [==============================] - 14s 2ms/step - loss: 0.3982 - accuracy: 0.8983\n",
      "Epoch 5/10\n",
      "6000/6000 [==============================] - 16s 3ms/step - loss: 0.3768 - accuracy: 0.9037\n",
      "Epoch 6/10\n",
      "6000/6000 [==============================] - 13s 2ms/step - loss: 0.3739 - accuracy: 0.9053\n",
      "Epoch 7/10\n",
      "6000/6000 [==============================] - 13s 2ms/step - loss: 0.3538 - accuracy: 0.9112\n",
      "Epoch 8/10\n",
      "6000/6000 [==============================] - 17s 3ms/step - loss: 0.3565 - accuracy: 0.9112\n",
      "Epoch 9/10\n",
      "6000/6000 [==============================] - 19s 3ms/step - loss: 0.3520 - accuracy: 0.9123\n",
      "Epoch 10/10\n",
      "6000/6000 [==============================] - 18s 3ms/step - loss: 0.3362 - accuracy: 0.9164\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x64072b8d0>"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(x_train_reshaped, y_train_cat, epochs=10, batch_size=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Making predictions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "predictions_vector = model.predict(x_test_reshaped)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [],
   "source": [
    "predictions = [np.argmax(pred) for pred in predictions_vector]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The label is 4\n",
      "The prediction is 4\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAOsAAADrCAYAAACICmHVAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAGCElEQVR4nO3dPWuUWQCG4Xf8iKSx0UUEMWmEKFg5gk0KJZWN6N9IZ6cWCtqJhV+FNqJiI4J2Iv4BCYOVIKiIQSFoLASVCRKdbbYQNnPiZGbMPDPX1T6+2cPCzREOwVqr1aqAwbdhvQ8A/BmxQgixQgixQgixQgixQohNnfzh7du3tyYnJ/t0FODdu3fV58+fayttHcU6OTlZNRqN3pwK+J96vd5289dgCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCCFWCLFpvQ8wCt6/f1/cDx8+3HZ78+ZNr48zMF68eFHcd+/e3XbbunVrr48z8NysEEKsEEKsEEKsEEKsEEKsEEKsEMI761/w9OnT4r60tPSXTjJYHjx4UNwXFxfbbtevX+/1cQaemxVCiBVCiBVCi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70CvxjtoZNyuEECuEECuEECuEECuEECuE8HRDUemfhFztn4ukt9ysEEKsEEKsEEKsEEKsEEKsEEKsEMI7K0XNZnPN346Pj/fwJLhZIYRYIYRYIYRYIYRYIYRYIYRYIYR3VoouXbrUdtu2bVvx22vXrvX6OCPNzQohxAohxAohxAohxAohxAohxAohvLNSNDMz03Y7depU8dupqaleH2ekuVkhhFghhFghhFghhFghhFghhFghhHdWim7fvr3eR+A/blYIIVYIIVYIIVYIIVYIIVYIIVYIIVYIIVYIIVYIIVYIIVYIIVYIIVYIIVYIIVYIIVYIIVYIIVYIIVYIIVYIIVYIUWu1Wn/+h2u1xaqq5vt3HBh5E61W65+Vho5iBdaPvwZDCLFCCLFCCLFCCLFCCLFCCLFCCLFCCLFCiH8BvQXTHH1izI0AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(x_test[4], cmap='Greys')\n",
    "plt.xticks([])\n",
    "plt.yticks([])\n",
    "print(\"The label is\", y_test[4])\n",
    "print(\"The prediction is\", predictions[4])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sometimes the model makes mistakes too."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The label is 3\n",
      "The prediction is 8\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAOsAAADrCAYAAACICmHVAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAH9ElEQVR4nO3dX2iN8QPH8eeMYVqR9qvVym9JSQ5OPM0ifyI3klKiKDdSWigkV0ukFEculNRwIVz405KMqyVS0pMLCa1o/pUyRQ7ZCc/v7ley53O25+zYPmfv1+1nz87TeHvk23EycRwHAEa/mpG+AQCDQ6yACWIFTBArYIJYARPECpgYP5QvbmhoiJubmyt0KwB6e3uDvr6+zEDbkGJtbm4OoiganrsC8JcwDBM3/hoMmCBWwASxAiaIFTBBrIAJYgVMECtgglgBE8QKmCBWwASxAiaIFTBBrIAJYgVMECtgglgBE8QKmCBWwASxAiaIFTBBrIAJYgVMECtgglgBE8QKmCBWwASxAiaIFTAxpA+mclYsFhO3z58/y2vPnDlT1mtv27ZN7k1NTWV9f4wNPFkBE8QKmCBWwASxAiaIFTBBrIAJYgVMVM05qzpHDYIg6OjoSNx279493LfzhyNHjsi9ra0tcTt69Ki8tq6uLtU9wQ9PVsAEsQImiBUwQayACWIFTBArYIJYARNVc856+vRpue/bt+8f3cnffv36JfdTp04lbp2dnfLaW7duyT2bzcodPniyAiaIFTBBrIAJYgVMECtgglgBE1VzdDNr1qzU12YyGbkfPHhQ7gsXLpR7qbfvbd++PXF79+6dvLalpUXu+Xw+9WsHQRDU1tbKHf8OT1bABLECJogVMEGsgAliBUwQK2CCWAETVXPOevny5dTX7tixQ+7t7e2pv/dgzJkzJ3FbunSpvPbjx49y37Vrl9x7enrkfuLEicRt3Lhx8loML56sgAliBUwQK2CCWAETxAqYIFbABLECJjJxHA/6i8MwjKMoquDtpFfqPak1Ncl/Lr1//15e29jYmOqehsOrV6/kvnfvXrnfvHmzrNffuXNn4nby5El5rfqZY2BhGAZRFA34m5mfJmCCWAETxAqYIFbABLECJogVMEGsgImqOWfdtGmT3K9du5a4ffr0SV47derUVPf0L/T29sp90aJFcu/r60v92vfv35f74sWLU3/vsYpzVqAKECtgglgBE8QKmCBWwASxAiaIFTBRNf9vsPq/d4NAn7OW686dO3I/e/as3Pfs2TOct/OHtrY2uR8+fDj193727JncOWcdXjxZARPECpggVsAEsQImiBUwQayAiao5uin10YjKy5cv5T59+nS5r1+/Xu7FYlHunZ2dch+tjh07JvdsNi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DJCpggVsAEsQImiBUwQayACWIFTBArYIJz1lFu5cqVZe2FQkHu/f39cj9//nzitnr1anntw4cP5Z7NZuU+e/bsxG3KlCny2vHjq++3Nk9WwASxAiaIFTBBrIAJYgVMECtgglgBE9V3GIU/1NfXl7Xv378/9WvncrnU1+JvPFkBE8QKmCBWwASxAiaIFTBBrIAJYgVMECtgglgBE8QKmCBWwASxAiaIFTBBrIAJYgVMECtgglgBE8QKmCBWwASxAiaIFTBBrIAJYgVMECtgglgBE8QKmCBWwASxAiaIFTBBrICJTBzHg//iTOZjEASvK3c7wJj33ziO/zPQMKRYAYwc/hoMmCBWwASxAiaIFTBBrIAJYgVMECtgglgBE8QKmPgfvn5sJTqVrvgAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(x_test[18], cmap='Greys')\n",
    "plt.xticks([])\n",
    "plt.yticks([])\n",
    "print(\"The label is\", y_test[18])\n",
    "print(\"The prediction is\", predictions[18])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Finding the accuracy of the model on the test set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The model is correct 9420 times out of 10000\n",
      "The accuracy is 0.942\n"
     ]
    }
   ],
   "source": [
    "num_correct = 0\n",
    "for i in range(len(predictions)):\n",
    "    if predictions[i] == y_test[i]:\n",
    "        num_correct += 1\n",
    "\n",
    "print(\"The model is correct\", num_correct, \"times out of\", len(y_test))\n",
    "print(\"The accuracy is\", num_correct/len(y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
